package com.xx.cloud.pay.service.impl;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.fastjson.JSONObject;
import com.xx.cloud.pay.config.HbTableStyle;
import com.xx.cloud.pay.entity.User;
import com.xx.cloud.pay.result.R;
import com.xx.cloud.pay.service.ExportService;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

@Service
public class ExportServiceImpl implements ExportService {

    @Override
    public R export(MultipartFile file) throws IOException {
        System.out.println(file.getSize());

        EasyExcel.read(file.getInputStream(), User.class, new ReadListener<User>() {
            @Override
            public void invoke(User data, AnalysisContext context) {
                // 处理每行读取的数据，例如添加到列表中
                // 注意：这里假设list是线程安全的，或者是在单线程环境中使用
                System.out.println("解析表格的数据" + data);
                //处理自己的逻辑
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                // 所有数据解析完成后的操作
            }
        }).sheet().doRead();
        // 注意：这里不再直接打印list，因为可能数据量很大，直接打印会消耗大量资源

        return R.ok("导入成功！");
    }

    @Override
    public void getStudent(List<String> list, HttpServletResponse response) {
        //这里我是写死的，你可以让前端传
//        Set<String> headList = new HashSet<>();
//        headList.add("name");
//        headList.add("age");


//        System.out.println(JSONObject.toJSON(headList));

        //数据
        List<User> dataList = new LinkedList<>();
        User student = new User();
        student.setName("张三");
        student.setSex("12");
        student.setPhone("12123213");
        dataList.add(student);
        User student1 = new User();
        student1.setName("李四");

        student1.setSex("13");
        student1.setPhone("32432432");
        dataList.add(student1);

        //直接从数据库查询的数据list

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
//        String fileName = URLEncoder.encode("学员基本信息", "UTF-8").replaceAll("\\+", "%20");
        // 设置Content-Disposition属性值
//        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        String fileName =  "simpleWrite" + System.currentTimeMillis() + ".xlsx";


//        EasyExcel.write(response.getOutputStream(), Student.class)
//                .includeColumnFieldNames(headList)
//                .registerWriteHandler(new HbTableStyle())
//                .registerWriteHandler(new HbTableStyle())
//                .registerWriteHandler(new HbTableStyle()).sheet("学员基本信息").doWrite(dataList);


        String filePath = "C:\\Users\\14772\\Downloads\\file.xlsx";
        EasyExcel.write(filePath, User.class)
                .includeColumnFieldNames(list)
//                .registerWriteHandler(new ExcelWidthStyleStrategy())
//                .registerWriteHandler(new ExcelHeightStyleStrategy())
                .registerWriteHandler(new HbTableStyle()).sheet("学员基本信息").doWrite(dataList);
//                .registerWriteHandler(new HbTableStyle()).sheet("学员基本信息").doWrite(dataList);
    }
}
